package com.fadadianqi.commons;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

import java.io.IOException;
import java.io.StringWriter;


public class JSONProvider {

    public static class TestClass {
        private String name;
        private String name2;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getName2() {
            return name2;
        }

        public void setName2(String name2) {
            this.name2 = name2;
        }
    }

    public static void main(String[] args) {
        //String json = "{\"clientType\":1,\"passwodsfddsrd\"sdfsxxxxxxx\",\"deviceToken\":\"00-99-dd-99-34-bb-pp-pp-we-we-cs-rf-gg\"}";
        TestClass tc = new TestClass();
        tc.setName("dddd");
        tc.setName2("2222222");
        System.out.println(JSONProvider.toJSON(tc));
    }

    private static ObjectMapper mapper = null;

    static {
        if (mapper == null) {
            JsonFactory factory = new JsonFactory();
            mapper = new ObjectMapper(factory);
            mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
        }
    }

    private static final Log log = LogFactory.getLog(JSONProvider.class);

    public static final String toJSON(Object bean) {
        boolean hasError = false;
        StringWriter sw = new StringWriter();
        try {
            JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
            mapper.writeValue(gen, bean);
            gen.close();
        } catch (JsonGenerationException e) {
            log.error(e.toString(), e);
            hasError = true;
        } catch (JsonMappingException e) {
            log.error(e.toString(), e);
            hasError = true;
        } catch (IOException e) {
            log.error(e.toString(), e);
            hasError = true;
        }
        if (hasError) {
            return null;
        } else {
            return sw.toString();
        }
    }

    public static final Object toBean(String json, Class<?> clazz) {
        if (json == null || json.length() == 0) {
            return null;
        }
        try {
            return mapper.readValue(json, clazz);
        } catch (JsonParseException e) {
            log.error(e + "\r\njson: " + json, e);
        } catch (JsonMappingException e) {
            log.error(e + "\r\njson: " + json, e);
        } catch (IOException e) {
            log.error(e + "\r\njson: " + json, e);
        } catch (Exception e) {
            log.error(e + "\r\njson: " + json, e);
        }
        return null;
    }

    /**
     * @param json
     * @param bean
     * @return
     * @see #toBean(String, Class)
     * @deprecated
     */
    public static final Object toBean(String json, Object bean) {
        return toBean(json, bean.getClass());
    }

}
